package com.administrator.cloudapplication.tools;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.administrator.cloudapplication.R;

/**
 * Created by Administrator on 2017/4/10.
 */

public class ViewUtils {
    public  static Dialog createDialog(Context context, String msg){
        LayoutInflater inflater=LayoutInflater.from(context);
        View v=inflater.inflate(R.layout.loading_dialog,null);
        LinearLayout layout=(LinearLayout) v.findViewById(R.id.dialog);
        ImageView imageView=(ImageView) v.findViewById(R.id.dialog_img);
        Animation anim= AnimationUtils.loadAnimation(context,R.anim.loading);
        imageView.startAnimation(anim);
        Dialog loadingDialog=new Dialog(context,R.style.loading_dialog);
        loadingDialog.setCanceledOnTouchOutside(true);
        loadingDialog.setCancelable(true);
        loadingDialog.getWindow().setGravity(Gravity.CENTER);
        int width=getScreenWidth((Activity) context)/3;
        loadingDialog.setContentView(layout,new  LinearLayout.LayoutParams(width,width/2));
                return loadingDialog;
    }
    /*
    取得屏幕宽度
     */
    public  static int getScreenWidth(Activity activity){
        DisplayMetrics dm=new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }
    public  static int getScreenHeight(Activity activity){
        DisplayMetrics dm=new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }
    /**
     * 将px值转换为dp值
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dp值转换为px值
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}
